﻿using AutoMapper;
using B.S.Packaged.Domain;
using B.S.Packaged.ErrorCode;
using B.S.Packaged.WebAPI.Write.Applications.Commands.PackagingPlanCommands;
using MediatR;
using SqlSugar;

namespace B.S.Packaged.WebAPI.Write.Applications.CommandHandlers.PackagingPlanCommandHandlers
{
    public class UpdatePackagingPlanStatusCommandHandler : IRequestHandler<UpdatePackagingPlanStatusCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public UpdatePackagingPlanStatusCommandHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<object>> Handle(UpdatePackagingPlanStatusCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.code = APIResultCode.success;
            result.msg = "执行成功";

            var model = await db.Queryable<PackagingPlan>()
                   .FirstAsync(p => p.PackagingPlanId == request.PackagingPlanId);
            if (model != null)
            {
                model.isload = 1;
                await db.Updateable<PackagingPlan>(model).ExecuteCommandAsync();
            }

            return await Task.FromResult(result);
        }
    }
}
